package com.yu.subject.common.util;

import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class BeanCopyUtil {

    public static <S, T> List<T> copyListProperties(List<S> sources, Class<T> targetClass) {
        if (CollectionUtils.isEmpty(sources)) {
            return Collections.emptyList();
        }
        return sources.stream()
            .map(source -> {
                try {
                    T target = targetClass.newInstance();
                    BeanUtils.copyProperties(source, target);
                    return target;
                } catch (InstantiationException | IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
            })
            .collect(Collectors.toList());
    }
}